package org.elasticsearch.commons;

import org.nlpcn.commons.lang.pinyin.PinyinFormat;
import org.nlpcn.commons.lang.pinyin.PinyinFormatter;
import org.nlpcn.commons.lang.tire.SmartGetWord;
import org.nlpcn.commons.lang.tire.domain.SmartForest;
import org.nlpcn.commons.lang.util.StringUtil;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

enum PinyinUtil {
    INSTANCE;

    public static final String PINYIN_MAPPING_FILE = "/pinyin.txt";
    public static final String POLYPHONE_MAPPING_FILE = "/polyphone.txt";
    public static final String EMPTY = "";
    public static final String SHARP = "#";
    public static final String EQUAL = "=";
    public static final String COMMA = ",";
    public static final String SPACE = " ";
    public static final char CJK_UNIFIED_IDEOGRAPHS_START = '一';
    public static final char CJK_UNIFIED_IDEOGRAPHS_END = '龥';
    private List<String> pinyinDict = null;
    private SmartForest<String[]> polyphoneDict = null;
    private int maxLen = 2;

    private PinyinUtil() {
        this.loadPinyinMapping();
        this.loadPolyphoneMapping();
    }

    public void loadPinyinMapping() {
        this.pinyinDict = new ArrayList();

        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new BufferedInputStream(this.getClass().getResourceAsStream("/pinyin.txt")), StandardCharsets.UTF_8));
            String line = null;

            while(null != (line = in.readLine())) {
                if (line.length() != 0 && !line.startsWith("#")) {
                    String[] pair = line.split("=");
                    if (pair.length < 2) {
                        this.pinyinDict.add("");
                    } else {
                        this.pinyinDict.add(pair[1]);
                    }
                }
            }

            in.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

    }

    public void loadPolyphoneMapping() {
        this.polyphoneDict = new SmartForest();

        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new BufferedInputStream(this.getClass().getResourceAsStream("/polyphone.txt")), StandardCharsets.UTF_8));
            String line = null;

            while(null != (line = in.readLine())) {
                if (line.length() != 0 && !line.startsWith("#")) {
                    String[] pair = line.split("=");
                    if (pair.length >= 2) {
                        this.maxLen = this.maxLen < pair[0].length() ? pair[0].length() : this.maxLen;
                        this.polyphoneDict.add(pair[0], pair[1].split(" "));
                    }
                }
            }

            in.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

    }

    public String[] toUnformattedPinyin(char ch) {
        if (ch >= 19968 && ch <= '龥') {
            String pinyinStr = (String)this.pinyinDict.get(ch - 19968);
            return pinyinStr.split(",");
        } else {
            return null;
        }
    }

    public String[] toFormattedPinyin(char ch, PinyinFormat format) {
        String[] pinyinStrArray = this.toUnformattedPinyin(ch);
        if (null == pinyinStrArray) {
            return null;
        } else {
            for(int i = 0; i < pinyinStrArray.length; ++i) {
                pinyinStrArray[i] = PinyinFormatter.formatPinyin(pinyinStrArray[i], format);
            }

            return pinyinStrArray;
        }
    }

    public String toPinyin(char ch) {
        String[] pinyinStrArray = this.toUnformattedPinyin(ch);
        return null != pinyinStrArray && pinyinStrArray.length > 0 ? pinyinStrArray[0] : null;
    }

    public String toPinyin(char ch, PinyinFormat format) {
        String[] pinyinStrArray = null;
        pinyinStrArray = this.toFormattedPinyin(ch, format);
        return null != pinyinStrArray && pinyinStrArray.length > 0 ? pinyinStrArray[0] : null;
    }

    public List<String> convert(String str, PinyinFormat format) {
        if (StringUtil.isBlank(str)) {
            return Collections.emptyList();
        } else {
            SmartGetWord<String[]> word = this.polyphoneDict.getWord(str);
            List<String> lists = new LinkedList();
            String temp = null;

            int beginOffe;
            int i;
            for(beginOffe = 0; (temp = word.getFrontWords()) != null; beginOffe = word.offe + temp.length()) {
                for(i = beginOffe; i < word.offe; ++i) {
                    lists.add(this.toPinyin(str.charAt(i), format));
                }

                String[] var11 = (String[])word.getParam();
                int var8 = var11.length;

                for(int var9 = 0; var9 < var8; ++var9) {
                    String t = var11[var9];
                    lists.add(PinyinFormatter.formatPinyin(t, format));
                }
            }

            if (beginOffe < str.length()) {
                for(i = beginOffe; i < str.length(); ++i) {
                    lists.add(this.toPinyin(str.charAt(i), format));
                }
            }

            return lists;
        }
    }

    public void insertPinyin(String word, String[] pinyins) {
        this.polyphoneDict.add(word, pinyins);
    }
}

